#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <QString>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>

PlayScene::PlayScene(int index)
{
    //音效
    QSound *backSound =new QSound(":/res/music/BackButtonSound.wav",this);
    QSound *flipSound =new QSound(":/res/music/ConFlipSound.wav",this);
    QSound *winSound =new QSound(":/res/muisc/LevelWinSound.wav",this);


    this->levelIndex=index;
    qDebug()<<"您进入的是第"<<this->levelIndex<<"关";
    //设置第三场景图片尺寸
    this->setFixedSize(640,1176);
    //设置标题
    this->setWindowTitle("Begin!");
    //设置图标
    this->setWindowIcon(QPixmap(":/res/image/Coin0001.png"));
    //创建菜单栏
    QMenuBar * bar=menuBar();
    QMenu * startMenu =bar->addMenu("begin");
    QAction * quitAction =startMenu->addAction("退出");
            connect(quitAction,&QAction::triggered,[=](){

        this->close();
    });
            //返回按钮
            MyPushButton * backBtn =new MyPushButton(":/res/image/BackButton.png",":/res/image/BackButtonSelected.png");

            backBtn->setParent(this);
            backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

            connect(backBtn,&MyPushButton::clicked,[=](){
                //播放音效
                backSound->play();

               // qDebug()<<"返回到选关场景";

                QTimer::singleShot(500,this,[=](){
                    emit this->ChooseSceneBack();
                });

            });



            //显示当前关卡号
            QLabel * label=new QLabel;
            label->setParent(this);
            QString str = QString("Level: %1").arg(this->levelIndex);
            label->setText(str);
            QFont font("华文新魏",40);
            label->setFont(font);
            label->setText(QString::number(this->levelIndex));
            //设置标签大小，位置
            label->setGeometry(QRect(50,this->height()-70,this->width(),70));
            //设置属性 51号
            label->setAttribute(Qt::WA_TransparentForMouseEvents);
            //胜利图片
            QLabel *winLabel =new QLabel;
            winLabel->setParent(this);
            QPixmap pix;
            pix.load(":/res/image/LevelCompletedDialogBg.png");
              winLabel->setPixmap(pix);
              winLabel->setGeometry(0,0,pix.width(),pix.height());
              winLabel->move(this->width()*0.5-pix.width()*0.5,-pix.height());



            //初始化关卡的二维数组
            dataConfig config;
            for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                this->gameArray[i][j]=config.mData[this->levelIndex][i][j];


            }


            }



              //创建金币背景图片
            for(int i=0;i<4;i++){

                for(int j=0;j<4;j++){
                    //绘制背景
                    QLabel* label=new QLabel;
                    QPixmap pix;
                    pix.load(":/res/image/BoardNode.png");
                    label->setGeometry(0,0,pix.width()*2,pix.height()*2);
                    label->setPixmap(pix);
                    label->setParent(this);
                    label->move(150+i*pix.width()*2,400+j*pix.height()*2);


                    //创建金币
                    QString str;
                    if(this->gameArray[i][j]==1){
                        str=":/res/image/Coin0001.png";

                    }
                    else{
                        str=":/res/image/Coin0008.png";

                    }
                    MyCoin *coin =new MyCoin(str);
                    coin->setParent(this);
                    coin->move(152+i*pix.width()*2,428+j*pix.height()*2);
                    coin->poxX=i;
                    coin->poxY=j;
                    coin->flag=gameArray[i][j];
                    this->coinBtn[i][j]=coin;

                    //点击硬币 调用 change flag
                    connect(coin,&MyCoin::clicked,[=]{
                       //播放音效
                        flipSound->play();

                        for(int i=0;i<4;i++){
                            for(int j=0;j<4;j++){

                                this->coinBtn[i][j]->isWin=true;

                            }

                        }


                        coin->changeFlag();
                        gameArray[i][j]=gameArray[i][j]==0?1:0;//同步二维数组，后期扩展->存档，读档
                     //翻转周围硬币
                        //延时翻转周围硬币
                        QTimer::singleShot(300,this,[=](){
                            //检测右侧硬币翻转
                              if(coin->poxX+1<=3){
                                  this->coinBtn[coin->poxX+1][coin->poxY]->changeFlag();
                                  gameArray[coin->poxX+1][coin->poxY]=gameArray[coin->poxX+1][coin->poxY]==0?1:1;
                              }
                              //检测左侧硬币翻转
                                if(coin->poxX-1>=0){
                                    this->coinBtn[coin->poxX-1][coin->poxY]->changeFlag();
                                    gameArray[coin->poxX-1][coin->poxY]=gameArray[coin->poxX-1][coin->poxY]==0?1:1;
                                }
                                //检测下侧硬币的翻转
                                if(coin->poxY+1<=3){
                                    this->coinBtn[coin->poxX][coin->poxY+1]->changeFlag();
                                    gameArray[coin->poxX][coin->poxY+1]=gameArray[coin->poxX][coin->poxY+1]==0?1:1;
                                }
                                //检测上侧
                                if(coin->poxY-1>=0){
                                    this->coinBtn[coin->poxX][coin->poxY-1]->changeFlag();
                                    gameArray[coin->poxX][coin->poxY-1]=gameArray[coin->poxX][coin->poxY-1]==0?1:1;
                                }
                   //检测游戏是否胜利
                                this->isWin=true;
                                for(int i=0;i<4;i++){
                                    for(int j=0;j<4;j++){
                                    if(this->coinBtn[i][j]->flag==false){
                                        this->isWin=false;
                                        break;

                                    }}

                                }
                                if(this->isWin){
                                    qDebug()<<"游戏胜利";
                                    //播放音效
                                    winSound->play();
                                    //将所有硬币的是否胜利改为真
                                    for(int i=0;i<4;i++){
                                        for(int j=0;j<4;j++){
                                            this->coinBtn[i][j]->isWin=true;

                                        }

                                    }
                                  //显示胜利图片
                                   QPropertyAnimation * animation =new QPropertyAnimation(winLabel,"geometry");
                                   animation->setDuration(1000);
                                   animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                                   animation->setEndValue(QRect(winLabel->x(),winLabel->y()+200,winLabel->width(),winLabel->height()));
                                    animation->setEasingCurve(QEasingCurve::OutBounce);
                                    animation->start(QAbstractAnimation::DeleteWhenStopped);
                                }
                                else{
                                    for(int i=0;i<4;i++){
                                        for(int j=0;j<4;j++){

                                            this->coinBtn[i][j]->isWin=false;

                                        }

                                    }

                                }


                        });

                    });
                }

            }
}

void PlayScene::paintEvent(QPaintEvent *)
{    //绘制背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/image/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
//绘制标题
    pix.load(":/res/image/Title.png");
    //缩放
    painter.drawPixmap(20,60,pix);



}
